<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class AdminModule extends Base
{
    /**
     * 添加模块
     */
    public function modAdd($data=array()){
        try {
            //添加模块数据
            if(!empty($data)){
                $res = $this->firstOrCreate($data);
            }else{
                $res = $this->save();
            }
            if($res){
                return [
                    'code'=>0,
                    'message'=>''
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }


    /**
     * 编辑模块
     */
    public function modUpdate($data=array()){
        try {
            //添加模块数据
            $model = self::find($data['id']);
            $model->modname = $data['modname'];
            $model->modspace = $data['modspace'];
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'message'=>''
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    /**
     * @param array $options  分页参数，含limit(每页数据条数)和page（当前页）
     * @param array $where  分页条件
     */
    public function getModPageList($options=[],$where=[]){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where)->orderBy('created_at','desc');
            $data = $this->paging($model,$page,$limit);
            return $data;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
